package au.com.bus4u.controller.admin;

import au.com.bus4u.controller.BaseController;
import au.com.bus4u.service.PermissionService;
import au.com.bus4u.service.RoleService;
import au.com.bus4u.service.UserService;
import au.com.bus4u.utils.JsonResult;
import au.com.bus4u.utils.PageData;
import com.github.pagehelper.PageInfo;
import lombok.extern.java.Log;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author
 * @since 2019-09-24
 */
@RestController
@RequestMapping("/admin/user")
@Log
public class AdminUserController extends BaseController {

	@Autowired
	private UserService userService;
	
	@Autowired
	private PermissionService permissionService;
	
	
	/**
	 * 查询
	 */
	@RequiresPermissions("user:view")
	@RequestMapping("/loadAllUser")
	public PageData loadAllUser() throws Exception{
		log.info("=========================================");

		setPageHelperStartPage();
		PageData pageData = getPageData();
		List<PageData> list = userService.list(pageData);
		PageInfo<PageData> pageInfo = new PageInfo<>(list);//得到分页的结果对象
		List<PageData> pageList = pageInfo.getList();//获取返回页面的分页list数据
		PageData page = getPage(pageInfo);//获取分页数据page

		PageData result = new PageData();
		result.put("code",0);
		result.put("msg","");
		result.put("data",pageList);
		result.put("page",page);
		result.put("count",page.get("total"));//总条数 layui数据表分页组件需要

		return result;
	}
	
	
	/**
	 * 添加
	 */
	@RequiresPermissions("user:add")
	@RequestMapping("/addUser")
	public JsonResult addUser() throws Exception {
		log.info("=========================================");
//		try {
//			roleVo.setCreatetime(new Date());
//			this.roleService.save(roleVo);
//			return JsonResult.ADD_SUCCESS;
//		} catch (Exception e) {
//			e.printStackTrace();
//			return JsonResult.ADD_ERROR;
//		}
		PageData pd = getPageData();
//		System.err.println("pd = " + pd);
		userService.save(pd);
		return new JsonResult(200,"新增成功");
	}
	/**
	 * 修改
	 */
	@RequiresPermissions("user:update")
	@RequestMapping("/updateUser")
	public JsonResult updateUser() throws Exception {
		log.info("=========================================");
//		try {
//			this.roleService.updateById(roleVo);
//			return JsonResult.UPDATE_SUCCESS;
//		} catch (Exception e) {
//			e.printStackTrace();
//			return JsonResult.UPDATE_ERROR;
//		}

		PageData pd = getPageData();
//		System.err.println("pd = " + pd);
		userService.edit(pd);
		return new JsonResult(200,"更新成功");
	}
	
	/**
	 * 删除
	 */
	@RequiresPermissions("user:delete")
	@RequestMapping("/deleteUser")
	public JsonResult deleteUser(String user_id) throws Exception {
		log.info("=========================================");
		log.info("user_id = " + user_id);
//		try {
//			this.roleService.removeById(id);
//			return JsonResult.DELETE_SUCCESS;
//		} catch (Exception e) {
//			e.printStackTrace();
//			return JsonResult.DELETE_ERROR;
//		}
		return null;
	}

}

